﻿using System;
using Community.StaticAnalysis.Design.Rules;
using Community.StaticAnalysis.Rules.Tests.Dummies;
using Microsoft.FxCop.Sdk;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Community.StaticAnalysis.Rules.Tests.Design
{
    /// <summary>
    ///This is a test class for MarkClassWithDataContractTest and is intended
    ///to contain all MarkClassWithDataContractTest Unit Tests
    ///</summary>
    [TestClass]
    public class MarkClassWithDataContractTest
    {
        /// <summary>
        ///A test for MarkClassWithDataContract Constructor
        ///</summary>
        [TestMethod]
        [DeploymentItem("Community.StaticAnalysis.Design.Rules.dll")]
        public void MarkClassWithDataContractConstructorTest()
        {
            RunRuleTest(typeof(DataContractAttributeMissing), true);
            RunRuleTest(typeof(ValidDataContract), false);
            RunRuleTest(typeof(DisposableObject), false);
        }

        /// <summary>
        /// Runs the rule test.
        /// </summary>
        /// <param name="testType">Type of the test.</param>
        /// <param name="problemsExpected">if set to <c>true</c> [problems expected].</param>
        private static void RunRuleTest(Type testType, Boolean problemsExpected)
        {
            MarkClassWithDataContract target = new MarkClassWithDataContract();
            TypeNode node = CcUtil.GetTypeNode(testType);
            ProblemCollection problems = target.Check(node);

            if (problemsExpected)
            {
                Assert.IsNotNull(problems, "Problems failed to be returned.");
                Assert.AreEqual(1, problems.Count, "Problems returned an incorrect Count value.");
                Assert.IsFalse(String.IsNullOrEmpty(problems[0].Resolution.Name), "Rule problem resolution does not return a value.");
                Assert.IsTrue(problems[0].Resolution.ToString().Contains(testType.Name), "Rule resolution value does not identify class by name.");   
            }
            else
            {
                Assert.IsTrue(problems == null || problems.Count == 0, "Problems were returned when none were expected");
            }
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get;
            set;
        }
    }
}